package com.ncst.backend.service.impl;

import com.ncst.backend.domain.*;
import com.ncst.backend.domain.vo.FeeGetVo;
import com.ncst.backend.mapper.*;
import com.ncst.backend.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处方服务实现类
 */
@Slf4j
@Service
public class PrescriptionServiceImpl implements PrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private CheckRequestMapper checkRequestMapper;
    @Autowired
    private DisposalRequestMapper disposalRequestMapper;
    @Autowired
    private MedicalTechnologyMapper medicalTechnologyMapper;
    @Autowired
    private DrugInfoMapper drugInfoMapper;

    @Override
    public Prescription getById(Integer id) {
        return prescriptionMapper.selectById(id);
    }

    @Override
    public List<Prescription> getAll() {
        return prescriptionMapper.selectAll();
    }

    @Override
    public List<Prescription> getByCondition(Prescription prescription) {
        return prescriptionMapper.selectByCondition(prescription);
    }

    @Override
    public List<Prescription> getByPage(Integer pageNum, Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        return prescriptionMapper.selectByPage(offset, pageSize);
    }

    @Override
    public List<Prescription> getByPage(Integer pageNum, Integer pageSize, Prescription prescription) {
        Integer offset = (pageNum - 1) * pageSize;
        List<Prescription> prescriptions = prescriptionMapper.selectByCondition(prescription);

        // 执行分页操作
        int startIndex = Math.min(offset, prescriptions.size());
        int endIndex = Math.min(offset + pageSize, prescriptions.size());
        return prescriptions.subList(startIndex, endIndex);
    }

    @Override
    public Long countAll() {
        return prescriptionMapper.countAll();
    }

    @Override
    public Long countByCondition(Prescription prescription) {
        return prescriptionMapper.countByCondition(prescription);
    }

    @Override
    public boolean save(Prescription prescription) {
        try {
            int result = prescriptionMapper.insert(prescription);
            return result > 0;
        } catch (Exception e) {
            log.error("保存处方失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean saveBatch(List<Prescription> list) {
        try {
            int result = prescriptionMapper.insertBatch(list);
            return result > 0;
        } catch (Exception e) {
            log.error("批量保存处方失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean updateById(Prescription prescription) {
        try {
            int result = prescriptionMapper.updateById(prescription);
            return result > 0;
        } catch (Exception e) {
            log.error("更新处方失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean removeById(Integer id) {
        try {
            int result = prescriptionMapper.deleteById(id);
            return result > 0;
        } catch (Exception e) {
            log.error("删除处方失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean removeByIds(List<Integer> ids) {
        try {
            int result = prescriptionMapper.deleteByIds(ids);
            return result > 0;
        } catch (Exception e) {
            log.error("批量删除处方失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public List<Prescription> getByCaseNumberAndRealName(String caseNumber, String realName) {
        Register register = new Register();
        register.setCaseNumber(caseNumber);
        register.setRealName(realName);
        List<Register> registers = registerMapper.selectByCondition(register);
        if (registers != null && !registers.isEmpty()) {
            Integer registerId = registers.get(0).getId();
            Prescription prescription = new Prescription();
            prescription.setRegisterId(registerId);
            return prescriptionMapper.selectByCondition(prescription);
        }
        return null;
    }

    /**
     * 查询费用
     *
     * @param registerId
     * @return
     */
    @Override
    public List<FeeGetVo> getFeeByRegisterId(Integer registerId) {
        // 检查申请费用
        List<CheckRequest> checkRequests = checkRequestMapper.selectByRegisterId(registerId);
        // 处置申请
        List<DisposalRequest> disposalRequests = disposalRequestMapper.getByRegisterId(registerId);
        // 处方申请
        List<Prescription> prescriptions = prescriptionMapper.getByRegisterId(registerId);
        List<FeeGetVo> feeGetVos = new ArrayList<>();
        for (CheckRequest checkRequest : checkRequests) {
            FeeGetVo feeGetVo = new FeeGetVo();
            feeGetVo.setRegisterId(checkRequest.getRegisterId());
//            feeGetVo.setName(checkRequest.getPatientName());
            feeGetVo.setCount(1);
            MedicalTechnology medicalTechnology = medicalTechnologyMapper.getById(checkRequest.getMedicalTechnologyId());
            feeGetVo.setName(medicalTechnology.getTechName());
            feeGetVo.setPrice(medicalTechnology.getTechPrice());
            feeGetVo.setJiner(medicalTechnology.getTechPrice());
            feeGetVo.setState(checkRequest.getCheckState());
            feeGetVos.add(feeGetVo);
        }
        for (DisposalRequest disposalRequest : disposalRequests) {
            FeeGetVo feeGetVo = new FeeGetVo();
            feeGetVo.setRegisterId(disposalRequest.getRegisterId());
            feeGetVo.setName(disposalRequest.getDisposalInfo());
            feeGetVo.setCount(1);
            MedicalTechnology medicalTechnology = medicalTechnologyMapper.getById(disposalRequest.getRegisterId());
            feeGetVo.setPrice(medicalTechnology.getTechPrice());
            feeGetVo.setJiner(medicalTechnology.getTechPrice());
            feeGetVo.setState(disposalRequest.getDisposalState());
            feeGetVos.add(feeGetVo);
        }
        for (Prescription prescription : prescriptions) {
            FeeGetVo feeGetVo = new FeeGetVo();
            feeGetVo.setRegisterId(prescription.getRegisterId());
            Integer drugId = prescription.getDrugId();
            DrugInfo drug = drugInfoMapper.selectById(drugId);
            feeGetVo.setName(drug.getDrugName());
            feeGetVo.setGuige(drug.getDrugFormat());
            feeGetVo.setCount(prescription.getDrugNumber());
            feeGetVo.setPrice(drug.getDrugPrice());
            feeGetVo.setJiner(drug.getDrugPrice());
            feeGetVo.setState(prescription.getDrugState());
            feeGetVos.add(feeGetVo);
        }
        return feeGetVos;
    }
}
